﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System.IO
{
    /// <summary>路径操作帮助</summary>
    public static class PathHelper
    {
        #region 路径操作辅助
        /// <summary>获取文件或目录的全路径，过滤相对目录</summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static String GetFullPath(this String path)
        {
            if (!Path.IsPathRooted(path)) path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);

            return Path.GetFullPath(path);
        }
        /// <summary>
        /// 确保目录
        /// </summary>
        /// <param name="path"></param>
        /// <param name="isfile"></param>
        /// <returns></returns>
        public static string EnsureDirectory(this string path, bool isfile = true)
        {
            string result;
            if (string.IsNullOrEmpty(path))
            {
                result = path;
            }
            else
            {
                path = path.GetFullPath();
                if (File.Exists(path) || Directory.Exists(path))
                {
                    result = path;
                }
                else
                {
                    string text = path;
                    if (text[text.Length - 1] == Path.PathSeparator)
                    {
                        text = Path.GetDirectoryName(path);
                    }
                    else
                    {
                        if (isfile)
                        {
                            text = Path.GetDirectoryName(path);
                        }
                    }
                    if (!string.IsNullOrEmpty(text) && !Directory.Exists(text))
                    {
                        Directory.CreateDirectory(text);
                    }
                    result = path;
                }
            }
            return result;
        }
        /// <summary>
        /// 文件路径合并
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static string CombinePath(this string path, params string[] ps)
        {
            string result;
            if (path == null || ps == null || ps.Length < 1)
            {
                result = path;
            }
            else
            {
                for (int i = 0; i < ps.Length; i++)
                {
                    string path2 = ps[i];
                    path = Path.Combine(path, path2);
                }
                result = path;
            }
            return result;
        }
        #endregion
    }
}